//
//  UserViewController.m
//  豆瓣2
//
//  Created by dllo on 15/12/19.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "UserViewController.h"
#import "MtViewController.h"
#import "myMovieViewController.h"
@interface UserViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.mytableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.mytableView.backgroundColor = [UIColor whiteColor];
    self.mytableView.delegate = self;
    self.mytableView.dataSource =self;
    [self.view addSubview:self.mytableView];
    self.navigationItem.title = @"我的";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtion:)];
    
}

- (void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    NSString *str = [[NSUserDefaults standardUserDefaults]objectForKey:@"bool"];
    if (str == nil) {
        
        [self.navigationItem.rightBarButtonItem setTitle:@"登陆"];
    }else{
        
        [self.navigationItem.rightBarButtonItem setTitle:@"注销"];
    }
}

- (void)rightButtion:(UIBarButtonItem *)bar{
    
    NSString *str = [[NSUserDefaults standardUserDefaults]objectForKey:@"bool"];
    
    if (str == nil) {
        
        MtViewController *mtVC = [[MtViewController alloc]init];
        UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:mtVC];
        [self presentViewController:naVC animated:YES completion:^{
            
            
        }];
     //   [[NSUserDefaults standardUserDefaults]setObject:@"no" forKey:@"bool"];
    }else{
        
         [self.navigationItem.rightBarButtonItem setTitle:@"登陆"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"bool"];
       
    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *index = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:index];
    if (!cell) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:index];
        
    }
    if (indexPath.section == 0) {
        cell.textLabel.text = @"我的活动";
    }if (indexPath.section == 1) {
        cell.textLabel.text = @"我的电影";
    }if (indexPath.section == 2) {
        cell.textLabel.text = @"清除缓存";
    }
    
    return cell;

    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *str = [[NSUserDefaults standardUserDefaults]objectForKey:@"bool"];
    
    if (indexPath.section==1) {
        if (str==nil) {
            MtViewController *mtVC = [[MtViewController alloc]init];
            UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:mtVC];
            [self presentViewController:naVC animated:YES completion:^{
                
                
            }];

        }else{
            myMovieViewController *myView = [[myMovieViewController alloc]init];
            
            [self.navigationController pushViewController:myView animated:YES];
            
        }
    }if (indexPath.section==2) {
        UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"提示" message:@"清除缓存" preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:aler animated:YES completion:^{
            
        }];
        
        UIAlertAction *a = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [aler addAction:a];
        
        UIAlertAction *b = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [aler addAction:b];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
